package net.dipper.tankwar.game.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.IOException;

import wnb.util.io.ImageManager;
import wnb.util.ui.Animation;

public class WelcomeAnimation implements Animation {
	public static final int DEFAULT_PADDING = 80;
	
	private String logo;
	private String title;
	
	public WelcomeAnimation(String title, String logo){
		this.title = title;
		this.logo = logo;
	}

	@Override
	public void paintFrame(Graphics2D g2, int width, int height, int frameIndex) {
		paintTitle(g2, width, height, frameIndex, title);
		paintLogo(g2, width, height, logo);
	}

	private void paintLogo(Graphics2D g2, int width, int height, String logo) {
		try {
			Image img = ImageManager.getImage(logo);
			int w = img.getWidth(null);
			int h = img.getHeight(null);
			int x = (width - w) / 2;
			int y = (height - DEFAULT_PADDING  - h) / 2 + 100;
			g2.drawImage(img, x, y, w, h, null);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void paintTitle(Graphics2D g2, int width, int height, int frameIndex, String title) {
		Font font = new Font("SansSerif", Font.BOLD, 50);
		FontMetrics metrics = g2.getFontMetrics(font);
		int strWidth = metrics.stringWidth(title);
		int x = (width - strWidth) / 2;
		g2.setFont(font);
		
		g2.setPaint(Color.BLACK);
		g2.drawString(title, x + 5, DEFAULT_PADDING + 5);
		
		int offset = ( 5 * frameIndex) % (2 * strWidth);
		offset = offset > strWidth ? 2 * strWidth - offset: offset;
		g2.setPaint(new GradientPaint(x + offset, 0, Color.GRAY, x + 20 + offset, 0, Color.DARK_GRAY));
		g2.drawString(title, x, DEFAULT_PADDING);
	}

	@Override
	public int getFrameCount() {
		return 0;
	}

}
